////////////////////////////////////////////////////////////////////////////////
// Filename: graphicsclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "graphicsclass.h"


GraphicsClass::GraphicsClass()
{
	m_D3D = 0;
	m_Camera = 0;
	//m_Model = 0;
	m_Sun_Model = 0;
	m_LightShader = 0;
	m_DepthShader = 0;	
	m_ShadowShader = 0;
	/*
	m_Light1 = 0;
	m_Light2 = 0;
	m_Light3 = 0;
	m_Light4 = 0;*/
}


GraphicsClass::GraphicsClass(const GraphicsClass& other)
{
}


GraphicsClass::~GraphicsClass()
{
}


bool GraphicsClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
{
	bool result;

		
	// Create the Direct3D object.
	m_D3D = new D3DClass;
	if(!m_D3D)
	{
		return false;
	}

	// Initialize the Direct3D object.
	result = m_D3D->Initialize(screenWidth, screenHeight, VSYNC_ENABLED, hwnd, FULL_SCREEN, SCREEN_DEPTH, SCREEN_NEAR);
	if(!result)
	{
		MessageBox(hwnd, L"Could not initialize Direct3D.", L"Error", MB_OK);
		return false;
	}

	// Create the camera object.
	m_Camera = new CameraClass;
	if(!m_Camera)
	{
		return false;
	}

	// Create the sunmodel object.
	m_Sun_Model = new ModelClass;
	if(!m_Sun_Model)
	{
		return false;
	}

	// Initialize the model object.
	result = m_Sun_Model->Initialize(m_D3D->GetDevice(), L"../Engine/data/SunTexture.png", "../Engine/data/sphere.txt");
	if(!result)
	{
		MessageBox(hwnd, L"Could not initialize the model object.", L"Error", MB_OK);
		return false;
	}
	// Create the sunmodel object.
	plane_model = new ModelClass;
	if(!plane_model)
	{
		return false;
	}

	// Initialize the model object.
	result = plane_model->Initialize(m_D3D->GetDevice(), L"../Engine/data/SunTexture.png", "../Engine/data/plane.txt");
	if(!result)
	{
		MessageBox(hwnd, L"Could not initialize the model object.", L"Error", MB_OK);
		return false;
	}
	// Create the light shader object.
	m_LightShader = new LightShaderClass;
	if(!m_LightShader)
	{
		return false;
	}

	// Initialize the light shader object.
	result = m_LightShader->Initialize(m_D3D->GetDevice(), hwnd);
	if(!result)
	{
		MessageBox(hwnd, L"Could not initialize the light shader object.", L"Error", MB_OK);
		return false;
	}

	sunsystems = new struct sunsystemstruct[numSunsystems];
	if(sunsystems==NULL)
		return false;
	for(int i = 0;i<numSunsystems;i++) {
		sunsystems[i].system.initialize(hwnd, m_D3D);
		for(int j = 0;j<3;j++) {
			float tempPos = (((float)rand()-(float)rand())/RAND_MAX) * 10.0f;
			switch(j) {
			case 0:
				sunsystems[i].pos.x = 0;//tempPos;
				break;
			case 1:
				sunsystems[i].pos.y = 0;//tempPos;
				break;
			default:
				sunsystems[i].pos.z = 0;//tempPos;
			}
		}
		for(int j = 0;j<6;j++) {
			sunsystems[i].pointlight[j] = new LightClass;
			if(!sunsystems[i].pointlight[j])
			{
				return false;
			}
			D3DXVECTOR4 sc = sunsystems[i].system.getSunColor();
			// Initialize the light object.
			sunsystems[i].pointlight[j]->SetDiffuseColor(sc.x, sc.y, sc.z,sc.w);
			sunsystems[i].pointlight[j]->SetAmbientColor(0.15f, 0.15f, 0.15f, 1.0f);
			sunsystems[i].pointlight[j]->SetPosition(sunsystems[i].pos.x,sunsystems[i].pos.y,sunsystems[i].pos.z);
		}
		//look at all Directions from Lightposition to simulate Pointlight
		sunsystems[i].pointlight[0]->SetLookAt(sunsystems[i].pos.x-1.0f,sunsystems[i].pos.y,sunsystems[i].pos.z);
		sunsystems[i].pointlight[0]->GenerateProjectionMatrix(SCREEN_DEPTH, SCREEN_NEAR);
		sunsystems[i].pointlight[1]->SetLookAt(sunsystems[i].pos.x+1.0f,sunsystems[i].pos.y,sunsystems[i].pos.z);
		sunsystems[i].pointlight[1]->GenerateProjectionMatrix(SCREEN_DEPTH, SCREEN_NEAR);

		sunsystems[i].pointlight[2]->SetLookAt(sunsystems[i].pos.x,sunsystems[i].pos.y-1.0f,sunsystems[i].pos.z);
		sunsystems[i].pointlight[2]->GenerateProjectionMatrix(SCREEN_DEPTH, SCREEN_NEAR);
		sunsystems[i].pointlight[3]->SetLookAt(sunsystems[i].pos.x,sunsystems[i].pos.y+1.0f,sunsystems[i].pos.z);
		sunsystems[i].pointlight[3]->GenerateProjectionMatrix(SCREEN_DEPTH, SCREEN_NEAR);

		sunsystems[i].pointlight[4]->SetLookAt(sunsystems[i].pos.x,sunsystems[i].pos.y,sunsystems[i].pos.z-1.0f);
		sunsystems[i].pointlight[4]->GenerateProjectionMatrix(SCREEN_DEPTH, SCREEN_NEAR);
		sunsystems[i].pointlight[5]->SetLookAt(sunsystems[i].pos.x,sunsystems[i].pos.y,sunsystems[i].pos.z+1.0f);
		sunsystems[i].pointlight[5]->GenerateProjectionMatrix(SCREEN_DEPTH, SCREEN_NEAR);
	}
	//Shadow
	// Create the render to texture object.
	for(int i = 0; i < pointLightShadownumber * numSunsystems; i++) {
		m_RenderTexture[i] = new RenderTextureClass;
		if(!m_RenderTexture)
		{
			return false;
		}

		// Initialize the render to texture object.
		result = m_RenderTexture[i]->Initialize(m_D3D->GetDevice(), SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT, SCREEN_DEPTH, SCREEN_NEAR);
		if(!result)
		{
			MessageBox(hwnd, L"Could not initialize the render to texture object.", L"Error", MB_OK);
			return false;
		}
	}
	// Create the depth shader object.
	m_DepthShader = new DepthShaderClass;
	if(!m_DepthShader)
	{
		return false;
	}
	
	// Initialize the depth shader object.
	result = m_DepthShader->Initialize(m_D3D->GetDevice(), hwnd);
	if(!result)
	{
		MessageBox(hwnd, L"Could not initialize the depth shader object.", L"Error", MB_OK);
		return false;
	}

	// Create the shadow shader object.
	m_ShadowShader = new ShadowShaderClass;
	if(!m_ShadowShader)
	{
		return false;
	}

	// Initialize the shadow shader object.
	result = m_ShadowShader->Initialize(m_D3D->GetDevice(), hwnd);
	if(!result)
	{
		MessageBox(hwnd, L"Could not initialize the shadow shader object.", L"Error", MB_OK);
		return false;
	}
	shadowInfo = new struct shadow;
	if(!shadowInfo)
	{
		return false;
	}
	return true;
}


void GraphicsClass::Shutdown()
{

	// Release the shadow shader object.
	if(m_ShadowShader)
	{
		m_ShadowShader->Shutdown();
		delete m_ShadowShader;
		m_ShadowShader = 0;
	}

	// Release the depth shader object.
	if(m_DepthShader)
	{
		m_DepthShader->Shutdown();
		delete m_DepthShader;
		m_DepthShader = 0;
	}

	// Release the light shader object.
	if(m_LightShader)
	{
		m_LightShader->Shutdown();
		delete m_LightShader;
		m_LightShader = 0;
	}

	// Release the sunmodel object.
	if(m_Sun_Model)
	{
		m_Sun_Model->Shutdown();
		delete m_Sun_Model;
		m_Sun_Model = 0;
	}
	// Release the camera object.
	if(m_Camera)
	{
		delete m_Camera;
		m_Camera = 0;
	}

	// Release the Direct3D object.
	if(m_D3D)
	{
		m_D3D->Shutdown();
		delete m_D3D;
		m_D3D = 0;
	}

	return;
}



bool GraphicsClass::Frame(float rotationY, float rotationZ, float forward, float right)
{
	// Set the position of the camera.
	m_Camera->SetMovement(forward,right);
	// Set the rotation of the camera.
	m_Camera->SetRotation(0.0f, rotationY, rotationZ);
	return Render();
}

bool GraphicsClass::Render()
{
	D3DXMATRIX worldMatrix, viewMatrix, projectionMatrix;
	D3DXMATRIX lightViewMatrix, lightProjectionMatrix;
	D3DXMATRIX lightViewMatrix2, lightProjectionMatrix2;
	D3DXMATRIX lightViewMatrix3, lightProjectionMatrix3;
	D3DXMATRIX lightViewMatrix4, lightProjectionMatrix4;
	bool result;
	generateShadowStruct();
	// Clear the buffers to begin the scene.
	m_D3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);

	// Generate the view matrix based on the camera's position.
	m_Camera->Render();
	// Generate the light view matrix based on the light's position.

	// Get the world, view, and projection matrices from the camera and d3d objects.
	m_D3D->GetWorldMatrix(worldMatrix);
	m_Camera->GetViewMatrix(viewMatrix);
	m_D3D->GetProjectionMatrix(projectionMatrix);

	renderUniverseScene(worldMatrix, viewMatrix, projectionMatrix);
	// Present the rendered scene to the screen.
	m_D3D->EndScene();

	return true;
}

float GraphicsClass::calcDistance(float x1,float y1,float z1, D3DXVECTOR3 pos2) {
	return sqrtf((x1-pos2.x)*(x1-pos2.x) + (y1-pos2.y)*(y1-pos2.y) + (z1-pos2.z)*(z1-pos2.z));
}

void GraphicsClass::renderUniverseScene(D3DXMATRIX &worldMatrix, D3DXMATRIX &viewMatrix, D3DXMATRIX &projectionMatrix) {
	float positionX, positionY, positionZ;
	// Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
	//m_Sun_Model->Render(m_D3D->GetDevice());
	float room = 75;
	for(int i = 0;i<numSunsystems;i++) {
		sunsystems[i].system.frame();
		//TODO Rene Testkasten
		D3DXMATRIX translationMatrix, scalingMatrix, rotationMatrix;
		D3DXMatrixTranslation(&translationMatrix, 0,-room,0); 
		D3DXMatrixScaling(&scalingMatrix,100,100,100);
		worldMatrix = scalingMatrix*translationMatrix;
		plane_model->Render(m_D3D->GetDevice());
		// Render the model using the light shader and the light arrays.
		m_ShadowShader->Render(m_D3D->GetDevice(), plane_model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, shadowInfo->lightViewMatrix, 
				shadowInfo->lightProjectionMatrix, plane_model->GetTexture(), shadowInfo->depthMapTexture,
				shadowInfo->lightPosition, sunsystems[0].pointlight[0]->GetAmbientColor(), shadowInfo->diffuseColor);
		
		D3DXMatrixTranslation(&translationMatrix, 0,room,0); 
		D3DXMatrixScaling(&scalingMatrix,100,100,100);
		D3DXMatrixRotationYawPitchRoll(&rotationMatrix,0,D3DX_PI,0);
		worldMatrix = scalingMatrix*rotationMatrix*translationMatrix;
		plane_model->Render(m_D3D->GetDevice());
		// Render the model using the light shader and the light arrays.
		m_ShadowShader->Render(m_D3D->GetDevice(), plane_model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, shadowInfo->lightViewMatrix, 
				shadowInfo->lightProjectionMatrix, plane_model->GetTexture(), shadowInfo->depthMapTexture,
				shadowInfo->lightPosition, sunsystems[0].pointlight[0]->GetAmbientColor(), shadowInfo->diffuseColor);
		
		D3DXMatrixTranslation(&translationMatrix, 0,0,-room); 
		D3DXMatrixScaling(&scalingMatrix,100,100,100);
		D3DXMatrixRotationYawPitchRoll(&rotationMatrix,0,D3DX_PI/2.0f,0);
		worldMatrix = scalingMatrix*rotationMatrix*translationMatrix;
		plane_model->Render(m_D3D->GetDevice());
		// Render the model using the light shader and the light arrays.
		m_ShadowShader->Render(m_D3D->GetDevice(), plane_model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, shadowInfo->lightViewMatrix, 
				shadowInfo->lightProjectionMatrix, plane_model->GetTexture(), shadowInfo->depthMapTexture,
				shadowInfo->lightPosition, sunsystems[0].pointlight[0]->GetAmbientColor(), shadowInfo->diffuseColor);

		D3DXMatrixTranslation(&translationMatrix, 0,0,room); 
		D3DXMatrixScaling(&scalingMatrix,100,100,100);
		D3DXMatrixRotationYawPitchRoll(&rotationMatrix,0,-D3DX_PI/2.0f,0);
		worldMatrix = scalingMatrix*rotationMatrix*translationMatrix;
		plane_model->Render(m_D3D->GetDevice());
		// Render the model using the light shader and the light arrays.
		m_ShadowShader->Render(m_D3D->GetDevice(), plane_model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, shadowInfo->lightViewMatrix, 
				shadowInfo->lightProjectionMatrix, plane_model->GetTexture(), shadowInfo->depthMapTexture,
				shadowInfo->lightPosition, sunsystems[0].pointlight[0]->GetAmbientColor(), shadowInfo->diffuseColor);

		D3DXMatrixTranslation(&translationMatrix, -room,0,0); 
		D3DXMatrixScaling(&scalingMatrix,100,100,100);
		D3DXMatrixRotationYawPitchRoll(&rotationMatrix,0,0,-D3DX_PI/2.0f);
		worldMatrix = scalingMatrix*rotationMatrix*translationMatrix;
		plane_model->Render(m_D3D->GetDevice());
		// Render the model using the light shader and the light arrays.
		m_ShadowShader->Render(m_D3D->GetDevice(), plane_model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, shadowInfo->lightViewMatrix, 
				shadowInfo->lightProjectionMatrix, plane_model->GetTexture(), shadowInfo->depthMapTexture,
				shadowInfo->lightPosition, sunsystems[0].pointlight[0]->GetAmbientColor(), shadowInfo->diffuseColor);

		D3DXMatrixTranslation(&translationMatrix, room,0,0); 
		D3DXMatrixScaling(&scalingMatrix,100,100,100);
		D3DXMatrixRotationYawPitchRoll(&rotationMatrix,0,0,D3DX_PI/2.0f);
		worldMatrix = scalingMatrix*rotationMatrix*translationMatrix;
		plane_model->Render(m_D3D->GetDevice());
		// Render the model using the light shader and the light arrays.
		m_ShadowShader->Render(m_D3D->GetDevice(), plane_model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, shadowInfo->lightViewMatrix, 
				shadowInfo->lightProjectionMatrix, plane_model->GetTexture(), shadowInfo->depthMapTexture,
				shadowInfo->lightPosition, sunsystems[0].pointlight[0]->GetAmbientColor(), shadowInfo->diffuseColor);
		//ENDE TODO Rene Testkasten
		for(int j = 0;j<sunsystems[i].system.getModelCount();j++) {
			sunsystems[i].system.getPlanetposition(positionX, positionY, positionZ, j);
			positionX+=sunsystems[i].pos.x;
			positionY+=sunsystems[i].pos.y;
			positionZ+=sunsystems[i].pos.z;
			// Move the model to the location it should be rendered at.
			D3DXMATRIX translationMatrix, scalingMatrix, rotation;
			D3DXMatrixTranslation(&translationMatrix, positionX, positionY, positionZ); 
			sunsystems[i].system.getPlanetrotation(j,rotation);
			D3DXMatrixScaling(&scalingMatrix, sunsystems[i].system.getPlanetScale(j), sunsystems[i].system.getPlanetScale(j), sunsystems[i].system.getPlanetScale(j));
			worldMatrix = scalingMatrix*rotation*translationMatrix;
			// Render the model using the light shader and the light arrays.
			sunsystems[i].system.getModel(j)->Render(m_D3D->GetDevice());
			m_ShadowShader->Render(m_D3D->GetDevice(), sunsystems[i].system.getModel(j)->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, shadowInfo->lightViewMatrix, 
				shadowInfo->lightProjectionMatrix, sunsystems[i].system.getModel(j)->GetTexture(), shadowInfo->depthMapTexture,
				shadowInfo->lightPosition, sunsystems[0].pointlight[0]->GetAmbientColor(), shadowInfo->diffuseColor);
		}
	}
}

void GraphicsClass::renderUniverseSceneShadowed(D3DXMATRIX &worldMatrix, D3DXMATRIX &lightViewMatrix, D3DXMATRIX &lightProjectionMatrix) {
	D3DXVECTOR4 diffuseColor[4];
	D3DXVECTOR3 lightPosition[4];
	
	float positionX, positionY, positionZ;
	float room = 75;
	// Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
	//TODO Rene Testkasten
		D3DXMATRIX translationMatrix, scalingMatrix, rotationMatrix;
		D3DXMatrixTranslation(&translationMatrix, 0,-room,0); 
		D3DXMatrixScaling(&scalingMatrix,100,100,100);
		worldMatrix = scalingMatrix*translationMatrix;
		plane_model->Render(m_D3D->GetDevice());
		// Render the model using the light shader and the light arrays.
		m_DepthShader->Render(m_D3D->GetDevice(), plane_model->GetIndexCount(), worldMatrix, lightViewMatrix, lightProjectionMatrix);
		
		D3DXMatrixTranslation(&translationMatrix, 0,room,0); 
		D3DXMatrixScaling(&scalingMatrix,100,100,100);
		D3DXMatrixRotationYawPitchRoll(&rotationMatrix,0,D3DX_PI,0);
		worldMatrix = scalingMatrix*rotationMatrix*translationMatrix;
		plane_model->Render(m_D3D->GetDevice());
		// Render the model using the light shader and the light arrays.
		m_DepthShader->Render(m_D3D->GetDevice(), plane_model->GetIndexCount(), worldMatrix, lightViewMatrix, lightProjectionMatrix);
		
		D3DXMatrixTranslation(&translationMatrix, 0,0,-room); 
		D3DXMatrixScaling(&scalingMatrix,100,100,100);
		D3DXMatrixRotationYawPitchRoll(&rotationMatrix,0,D3DX_PI/2.0f,0);
		worldMatrix = scalingMatrix*rotationMatrix*translationMatrix;
		plane_model->Render(m_D3D->GetDevice());
		// Render the model using the light shader and the light arrays.
		m_DepthShader->Render(m_D3D->GetDevice(), plane_model->GetIndexCount(), worldMatrix, lightViewMatrix, lightProjectionMatrix);

		D3DXMatrixTranslation(&translationMatrix, 0,0,room); 
		D3DXMatrixScaling(&scalingMatrix,100,100,100);
		D3DXMatrixRotationYawPitchRoll(&rotationMatrix,0,-D3DX_PI/2.0f,0);
		worldMatrix = scalingMatrix*rotationMatrix*translationMatrix;
		plane_model->Render(m_D3D->GetDevice());
		// Render the model using the light shader and the light arrays.
		m_DepthShader->Render(m_D3D->GetDevice(), plane_model->GetIndexCount(), worldMatrix, lightViewMatrix, lightProjectionMatrix);

		D3DXMatrixTranslation(&translationMatrix, -room,0,0); 
		D3DXMatrixScaling(&scalingMatrix,100,100,100);
		D3DXMatrixRotationYawPitchRoll(&rotationMatrix,0,0,-D3DX_PI/2.0f);
		worldMatrix = scalingMatrix*rotationMatrix*translationMatrix;
		plane_model->Render(m_D3D->GetDevice());
		// Render the model using the light shader and the light arrays.
		m_DepthShader->Render(m_D3D->GetDevice(), plane_model->GetIndexCount(), worldMatrix, lightViewMatrix, lightProjectionMatrix);

		D3DXMatrixTranslation(&translationMatrix, room,0,0); 
		D3DXMatrixScaling(&scalingMatrix,100,100,100);
		D3DXMatrixRotationYawPitchRoll(&rotationMatrix,0,0,D3DX_PI/2.0f);
		worldMatrix = scalingMatrix*rotationMatrix*translationMatrix;
		plane_model->Render(m_D3D->GetDevice());
		// Render the model using the light shader and the light arrays.
		m_DepthShader->Render(m_D3D->GetDevice(), plane_model->GetIndexCount(), worldMatrix, lightViewMatrix, lightProjectionMatrix);
		//ENDE TODO Rene Testkasten
	for(int i = 0;i<numSunsystems;i++) {
		sunsystems[i].system.frame();
		//Render Sun
		D3DXMATRIX translationMatrix, scalingMatrix;
		D3DXMatrixTranslation(&translationMatrix, sunsystems[i].pos.x, sunsystems[i].pos.y, sunsystems[i].pos.z); 
		D3DXMatrixScaling(&scalingMatrix, sunsystems[i].system.getSunScale(), sunsystems[i].system.getSunScale(), sunsystems[i].system.getSunScale());
		worldMatrix = scalingMatrix*translationMatrix;
		// Render the model using the DepthShader
		//m_Sun_Model->Render(m_D3D->GetDevice());
		//m_DepthShader->Render(m_D3D->GetDevice(), m_Sun_Model->GetIndexCount(), worldMatrix, lightViewMatrix, lightProjectionMatrix);
		for(int j = 0;j<sunsystems[i].system.getModelCount();j++) {
			sunsystems[i].system.getPlanetposition(positionX, positionY, positionZ, j);
			positionX+=sunsystems[i].pos.x;
			positionY+=sunsystems[i].pos.y;
			positionZ+=sunsystems[i].pos.z;
			// Move the model to the location it should be rendered at.
			D3DXMATRIX translationMatrix, scalingMatrix, rotation;
			D3DXMatrixTranslation(&translationMatrix, positionX, positionY, positionZ); 
			sunsystems[i].system.getPlanetrotation(j,rotation);
			D3DXMatrixScaling(&scalingMatrix, sunsystems[i].system.getPlanetScale(j), sunsystems[i].system.getPlanetScale(j), sunsystems[i].system.getPlanetScale(j));
			worldMatrix = scalingMatrix*rotation*translationMatrix;
			// Render the model using the Depth Shader
			sunsystems[i].system.getModel(j)->Render(m_D3D->GetDevice());
			m_DepthShader->Render(m_D3D->GetDevice(), sunsystems[i].system.getModel(j)->GetIndexCount(), worldMatrix, lightViewMatrix, lightProjectionMatrix);
		}
	}
}
void GraphicsClass::generateShadowStruct() {
	int i = 0;
	for(int j = 0;j < numSunsystems;j++) {
		for(int k=0;k<6;k++) {
			//Lightinfo
			shadowInfo->pointlight[i] = sunsystems[j].pointlight[k];
			//Lightviewmatrix
			shadowInfo->lightViewMatrix[i] = new D3DXMATRIX;
			sunsystems[j].pointlight[k]->GenerateViewMatrix();
			sunsystems[j].pointlight[k]->GetViewMatrix(*shadowInfo->lightViewMatrix[i]);
			//Projectionmatrix
			shadowInfo->lightProjectionMatrix[i] = new D3DXMATRIX;
			sunsystems[j].pointlight[k]->GetProjectionMatrix(*shadowInfo->lightProjectionMatrix[i]);
			//Lightposition
			shadowInfo->lightPosition[i] = sunsystems[j].pointlight[k]->GetPosition();
			//Lightcolor
			shadowInfo->diffuseColor[i] = sunsystems[j].pointlight[k]->GetDiffuseColor();
			//Depthmap
			RenderSceneToTexture(i);
			i++;
		}
	}
}
bool GraphicsClass::RenderSceneToTexture(int mapnumber)
{
	D3DXMATRIX worldMatrix, lightViewMatrix, lightProjectionMatrix, translateMatrix;

	// Set the render target to be the render to texture.
	m_RenderTexture[mapnumber]->SetRenderTarget(m_D3D->GetDevice());

	// Clear the render to texture.
	m_RenderTexture[mapnumber]->ClearRenderTarget(m_D3D->GetDevice(), 0.0f, 0.0f, 0.0f, 1.0f);
	// Get the world matrix from the d3d object.
	m_D3D->GetWorldMatrix(worldMatrix);

	// Get the view and orthographic matrices from the light object.
	lightViewMatrix = *shadowInfo->lightViewMatrix[mapnumber];
	lightProjectionMatrix = *shadowInfo->lightProjectionMatrix[mapnumber];
	renderUniverseSceneShadowed(worldMatrix, lightViewMatrix, lightProjectionMatrix);
	shadowInfo->depthMapTexture[mapnumber] = m_RenderTexture[mapnumber]->GetShaderResourceView();
	// Reset the render target back to the original back buffer and not the render to texture anymore.
	m_D3D->SetBackBufferRenderTarget();

	// Reset the viewport back to the original.
	m_D3D->ResetViewport();

	return true;
}